﻿package igz.fleaxy.air 
{
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Bitmap;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import igz.fleaxy.locale.LocaleManager;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class SystemTryManager {

		public var $SysTray : SystemTrayIcon;
		private var _SystemTrayMenu : NativeMenu;
		private var _MainWindowManager : MainWindowManager;
		
		private var _TimerDoubleClick : Timer;
		private var _ClickCount : uint;

		protected var _Settings:Object = { "tooltip" : "SystenTryTooltip"
										 , "icon" : null
										 , "onDoubleClick" : null 
										 , "onMouseDown" : null 
										 };
		public function SystemTryManager(p_mainWindowManager:MainWindowManager ) {
			_MainWindowManager = p_mainWindowManager;
		}

		/**
		 * 
		 * @param	p_settings : { "tooltip" : "SystenTryTooltip" }
		 * @param	p_nativeMenuItems : Array -> NativeMenuItem 
		 * @example var nativeMenuItem:NativeMenuItem = new NativeMenuItem("Buscar actualizaciones");
		 *			nativeMenuItem.addEventListener(Event.SELECT, _SearchUpdates_OnSelect);
		 * 
		 * 			$Init({ "tooltip" : "Mi Aplicación" }, [nativeMenuItem] )
		 */
		public function $Init(p_settings:Object, p_nativeMenuItems:Array = null ) : void {

			ObjectUtil.$Merge(p_settings, _Settings);

			if ( p_nativeMenuItems == null ) {
				p_nativeMenuItems = new Array();
			}

			if ( NativeApplication.supportsSystemTrayIcon && _Settings["icon"] != null ) {
				$SysTray = NativeApplication.nativeApplication.icon as SystemTrayIcon;
				$SysTray.bitmaps = [ (_Settings["icon"] as Bitmap) ];
				$SysTray.tooltip = _Settings["tooltip"];

				if ( NativeWindow.supportsMenu ) {
					_SystemTrayMenu = new NativeMenu();

					// Creamos opción "Cerrar" por defecto y es siempre la última
					var nativeMenuItem:NativeMenuItem = new NativeMenuItem( LocaleManager.$GetText("GENERAL","CLOSE") );
					nativeMenuItem.addEventListener(Event.SELECT, _OnMenuCloseSelected);
					p_nativeMenuItems.push(nativeMenuItem);

					if ( p_nativeMenuItems != null ) {
						for (var f:uint = 0; f < p_nativeMenuItems.length; f++ ) {
							_SystemTrayMenu.addItem( p_nativeMenuItems[f] );
						}
					}

					$SysTray.menu = _SystemTrayMenu;
					
					if ( _Settings["onDoubleClick"] is Function ) {
						$SysTray.addEventListener(MouseEvent.CLICK, _Systray_OnClick, false, 0, true);
						
						_TimerDoubleClick = new Timer(300, 1);
						_TimerDoubleClick.addEventListener( TimerEvent.TIMER, _TimerDoubleClick_OnTimer, false, 0, true );
					}

					if ( _Settings["onMouseDown"] is Function ) {
						$SysTray.addEventListener(MouseEvent.MOUSE_DOWN, _Systray_OnMouseDown, false, 0, true);
					}
				}
			}
			else {
				trace("[WARNING][SystemTryManager.$Init] No se ha definido ningún icono en el 'System Try' -> { 'icon' : bitmap }")
			}
		}
		
		private function _Systray_OnClick ( p_mouseEvent:MouseEvent ) : void {
			
			if ( !_TimerDoubleClick.running ) {
				_TimerDoubleClick.reset();
				_TimerDoubleClick.start();
			}
			
			_ClickCount++;
		}
		
		protected function _TimerDoubleClick_OnTimer ( p_timerEvent:TimerEvent ) : void {
			if ( _ClickCount >= 2 ) {
				_MainWindowManager.$ShowApp();
				_Settings["onDoubleClick"]();
			}
			_ClickCount = 0;
		}
		
		private function _Systray_OnMouseDown( p_mouseEvent:MouseEvent ) : void {
			_Settings["onMouseDown"]();
		}

		protected function _OnMenuCloseSelected( p_event:Event ):void {
			_MainWindowManager.$CloseApp();
		}
	}

}